package chapter21.others;

/**
 * Created by lzy
 * Date: 2018/6/2
 */
public class LiftOff implements Runnable {
    private static int taskCount = 0;
    private final int id = taskCount++;
    protected int countDown = 10;

    public LiftOff() {
    }

    public LiftOff(int countDown) {
        this.countDown = countDown;
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new LiftOff(10));
        Thread t2 = new Thread(new LiftOff(20));
        Thread t3 = new Thread(new ExceptionThread());
        Thread t4 = new Thread(new LiftOff(50));

        t1.start();
        t2.start();
        t3.start();
        t4.start();

        /*
        ExecutorService exec = Executors.newSingleThreadExecutor();
        exec.execute(new ExceptionThread());
        for (int i = 0; i < 4; i++) {
            exec.execute(new LiftOff());
        }
        exec.shutdown();
        */
    }

    public String status() {
        return "#" + this.id + "(" + (countDown > 0 ? countDown : "Liftoff!") + ")";
    }

    @Override
    public void run() {
        while (countDown-- > 0) {
            System.out.println(status());
            Thread.yield();
        }
    }
}
